﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Utils
{
    public class FileHelp
    {
        /// <summary>
        /// 删除文件夹及其子文件夹
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static bool DeleteDir(string file)
        {

            try
            {

                //去除文件夹和子文件的只读属性
                //去除文件夹的只读属性
                System.IO.DirectoryInfo fileInfo = new DirectoryInfo(file);
                fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;

                //去除文件的只读属性
                System.IO.File.SetAttributes(file, System.IO.FileAttributes.Normal);

                //判断文件夹是否还存在
                if (Directory.Exists(file))
                {

                    foreach (string f in Directory.GetFileSystemEntries(file))
                    {

                        if (File.Exists(f))
                        {
                            //如果有子文件删除文件
                            File.Delete(f);
                            Console.WriteLine(f);
                        }
                        else
                        {
                            //循环递归删除子文件夹
                            DeleteDir(f);
                        }

                    }

                    //删除空文件夹

                    Directory.Delete(file);

                }
                return true;

            }
            catch (Exception ) // 异常处理
            {
                return false;
            }

        }



        public static bool CopyDir(string sourcePath, string targetPath, ref string error)
        {
            if (string.IsNullOrEmpty(error))
            {
                error = "";
            }

            try
            {
                if (!Directory.Exists(sourcePath))
                {
                    error = "源文件不存在";
                    return false;
                }
                if (!Directory.Exists(targetPath))
                {
                    Directory.CreateDirectory(targetPath);
                }

                foreach (string f in Directory.GetFileSystemEntries(sourcePath))
                {
                    if (File.Exists(f))
                    {
                        //拷贝文件
                        FileInfo fileInfo = new FileInfo(f);
                        //如果有子文件删除文件
                        string targetFilePath = $"{targetPath}\\{fileInfo.Name}";
                        File.Copy(fileInfo.FullName, targetFilePath, true);
                    }
                    else
                    {
                        //拷贝文件夹
                        DirectoryInfo directoryInfo = new DirectoryInfo(f);
                        string targetDirectoryPath = $"{targetPath}\\{directoryInfo.Name}";
                        CopyDir(directoryInfo.FullName, targetDirectoryPath, ref error);
                    }
                }

                return true;
            }
            catch (Exception e)
            {
                error += e.Message;
                return false;
            }
        }



    }
}
